$(function () {
  let $edit = $('.btn-edit');
  let $del = $('.btn-del');
  let $add = $('#btn-add-tag');

  $del.click(del_tag);
  $add.click(add_tag);
  $edit.click(edit_tag);

  //用于删除文章标签的函数
  function del_tag() {
    let $this = $(this);
    let id = $(this).parent().parent().data('id');
    let name = $(this).parent().parent().data('name');
    fAlert.alertConfirm({
      title: `确定删除<${name}>标签`,
      text: '该操作不可撤销',
      type: 'error',
      confirmCallback: function () {
        selfAjax(
            `/admin/tags/${id}/`,
            'DELETE',
            '',
            function (res) {
              if (res.no === "0") {
                $this.parent().parent().remove();
              } else {
                swal({
                  title: res.msg,
                  type: 'error',
                  timer: 1000,
                  showConfirmButton: false
                });
              }
            }
        )

      }

    });
  }

  //用于添加标签
  function add_tag() {
    fAlert.alertOneInput({
      title: "请输入文章标签",
      text: "长度限制在20字以内",
      placeholder: "请输入文章标签",
      confirmCallback: function confirmCallback(inputVal) {
        //去除头尾的空白符
        inputVal = $.trim(inputVal);
        if (inputVal.length > 20 || inputVal === " ") {
          swal.showInputError("标签名长度有误")
        }
        selfAjax(
            `/admin/tags/`,
            'POST',
            JSON.stringify({'name': inputVal}),
            function (res) {
              if (res.no === "0") {
                swal({
                  title: res.msg,
                  type: 'success',
                  timer: 1000,
                  showConfirmButton: false
                });
                setTimeout(() => {
                  window.location.reload()
                }, 1000)
              } else {
                swal.showInputError(res.msg)
              }
            })
      }
    })

  }

  //用于编辑标签
  function edit_tag() {
    let $this = $(this);
    let id = $(this).parent().parent().data('id');
    let name = $(this).parent().parent().data('name');
    fAlert.alertOneInput({
      title: "请编辑文章标签",
      text: "长度限制在20字以内",
      value: name,
      confirmCallback: function confirmCallback(inputVal) {
        //去除头尾的空白符
        inputVal = $.trim(inputVal);
        if (inputVal.length > 20 || inputVal === " ") {
          swal.showInputError("标签名长度有误");
          return
        }
        if (name === inputVal) {
          swal.showInputError("标签名未变化");
          return
        }
        selfAjax(`/admin/tags/${id}/`,
            'PUT',
            JSON.stringify({'name': inputVal}),
            (res) => {
              if (res.no === "0") {
                $this.parent().siblings().eq(0).html(inputVal);
                swal({
                  title: res.msg,
                  type: 'success',
                  timer: 1000,
                  showConfirmButton: false
                });
              } else {
                swal({
                  title: res.msg,
                  type: 'error',
                  timer: 1000,
                  showConfirmButton: false
                });
              }
            }
        )

      }
    })
  }

  csrf()
});